﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Threading;

namespace SLPropertyGrid
{
	public abstract class ValueEditorBase : ContentControl, IPropertyValueEditor
	{
        public static int _counter = 0;
		protected ValueEditorBase() { }

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="label">The associated label for this Editor control</param>
		/// <param name="property">The associated PropertyItem for this control</param>
		public ValueEditorBase(TextBox label, PropertyItem property)
		{
            int unique = Interlocked.Increment(ref _counter);
			this.Label = label;
            this.Label.Name = "lbl" + property.Name + unique.ToString();
			this.Label.GotFocus += new RoutedEventHandler(Label_GotFocus);

			this.Name = "txt" + property.Name + unique.ToString();
			this.Property = property;
			this.BorderThickness = new Thickness(0);
			this.Margin = new Thickness(0);
			this.HorizontalAlignment = HorizontalAlignment.Stretch;
			this.HorizontalContentAlignment = HorizontalAlignment.Stretch;
		}

		protected override void OnGotFocus(RoutedEventArgs e)
		{
			if (null == Label)
				return;
			Debug.WriteLine("Control: " + this.Name + " getting focus");
			base.OnGotFocus(e);
			Label.Background = new SolidColorBrush(Color.FromArgb(255, 128, 202, 244));
		}

		protected override void OnLostFocus(RoutedEventArgs e)
		{
			if (null == Label)
				return;
			Debug.WriteLine("Control: " + this.Name + " losing focus");
			base.OnLostFocus(e);
			Label.Background = new SolidColorBrush(Colors.White);
		}

		private void Label_GotFocus(object sender, RoutedEventArgs e)
		{
			Debug.WriteLine("Label: " + this.Label.Name + " getting focus");
			this.Focus();
		}

		#region IPropertyValueEditor Members
		/// <summary>
		/// Gets the associated label for this Editor control
		/// </summary>
		public TextBox Label { get; private set; }
		/// <summary>
		/// Gets the associated PropertyItem for this control
		/// </summary>
		public PropertyItem Property { get; private set; }
		#endregion
	}
}
